<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 增加删除用户 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 增加删除用户</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<h2 id="1-增加用户"><a href="#1-增加用户" class="headerlink" title="1.增加用户"></a>1.增加用户</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：useradd</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>语法：</strong>    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">useradd [-u UID] [-g GID] [-d HOME] [-M] [-s]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>参数：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>-u</strong>：自定义UID</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>-g</strong>：使其属于已经存在的某个组，后面可以分组 id ，也可以分组名</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>-d</strong>：自定义用户的家目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>-M</strong>：不建立家目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>-s</strong>：自定义 shell</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;‘useradd’ 不加任何选项直接跟用户名，则会创建一个跟用户名同样名字的组。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># useradd test10</span></div><div class="line">[root@localhost ~]<span class="comment"># tail -n1 /etc/passwd</span></div><div class="line">test10:x:500:503::/home/test10:/bin/bash</div><div class="line">[root@localhost ~]<span class="comment"># tail -n1 /etc/group</span></div><div class="line">test10:x:503:</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;‘-g’ 选项后面跟一个不存在的gid会报错，提示该组不存在。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># useradd -u510 -g 513 -M -s /sbin/nologin user11</span></div><div class="line">useradd: group <span class="string">'513'</span> does not exist</div><div class="line">[root@localhost ~]<span class="comment"># useradd -u510 -g 502 -M -s /sbin/nologin user11</span></div><div class="line">[root@localhost ~]<span class="comment"># useradd -u511 -g grptest1 user12</span></div><div class="line">[root@localhost ~]<span class="comment"># tail -n2 /etc/passwd</span></div><div class="line">user11:x:510:502::/home/user11:/sbin/nologin</div><div class="line">user12:x:511:502::/home/user12:/bin/bash</div><div class="line">[root@localhost ~]<span class="comment"># tail -n2 /etc/group</span></div><div class="line">grptest1:x:502:</div><div class="line">test10:x:503:</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;‘-M’ 选项加上后则不建立用户家目录，但是在/etc/passwd文件中仍然有这个字段。但是使用 <code>ls /home/user11</code> 查看一下会提示该目录不存在。所以 ‘-M’ 选项的作用只是不创建那个目录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls /home/user11</span></div><div class="line">ls: 无法访问/home/user11: 没有那个文件或目录</div></pre></td></tr></table></figure>
<h2 id="2-删除账户"><a href="#2-删除账户" class="headerlink" title="2.删除账户"></a>2.删除账户</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：useradd</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>语法：</strong>  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">userdel [-r] username</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;‘-r’ 选项的作用只有一个，就是删除账户的时候连带账户的家目录一起删除。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls -ld /home/user12</span></div><div class="line">drwx------ 3 user12 grptest1 4096 5月  11 07:12 /home/user12</div><div class="line">[root@localhost ~]<span class="comment"># userdel user12</span></div><div class="line">[root@localhost ~]<span class="comment"># ls -ld /home/user12</span></div><div class="line">drwx------ 3 511 grptest1 4096 5月  11 07:12 /home/user12</div><div class="line">[root@localhost ~]<span class="comment"># ls -ld /home/test10/</span></div><div class="line">drwx------ 3 test10 test10 4096 5月  11 07:09 /home/test10/</div><div class="line">[root@localhost ~]<span class="comment"># userdel  -r test10</span></div><div class="line">[root@localhost ~]<span class="comment"># ls -ld /home/test10/</span></div><div class="line">ls: 无法访问/home/test10/: 没有那个文件或目录</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/9. Linux 增加删除用户/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
